package co.yixiang.modules.supplier.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.supplier.domain.SupplierUser;
import co.yixiang.modules.supplier.service.SupplierUserService;
import co.yixiang.modules.supplier.service.dto.SupplierUserQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2021-03-26
*/
@Api(tags = "代理商用户管理")
@RestController
@RequestMapping("/api/supplierUser")
public class SupplierUserController {

    private final SupplierUserService supplierUserService;

    public SupplierUserController(SupplierUserService supplierUserService) {
        this.supplierUserService = supplierUserService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('supplierUser:list')")
    public void download(HttpServletResponse response, SupplierUserQueryCriteria criteria) throws IOException {
        supplierUserService.download(supplierUserService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询代理商用户")
    @ApiOperation("查询代理商用户")
    @PreAuthorize("@el.check('supplierUser:list')")
    public ResponseEntity<Object> getSupplierUsers(SupplierUserQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(supplierUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增代理商用户")
    @ApiOperation("新增代理商用户")
    @PreAuthorize("@el.check('supplierUser:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody SupplierUser resources){
        return new ResponseEntity<>(supplierUserService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改代理商用户")
    @ApiOperation("修改代理商用户")
    @PreAuthorize("@el.check('supplierUser:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody SupplierUser resources){
        supplierUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除代理商用户")
    @ApiOperation("删除代理商用户")
    @PreAuthorize("@el.check('supplierUser:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        supplierUserService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}